package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.exception.ManagerServiceException;
import com.itany.mapper.ManagerUserMapper;
import com.itany.pojo.ManagerUser;
import com.itany.pojo.ManagerUserExample;
import com.itany.pojo.Role;
import com.itany.service.ManagerUserService;
import com.itany.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class ManagerUserServiceImpl implements ManagerUserService {

    @Autowired
    ManagerUserMapper mapper;

    public ManagerUser login(String username, String password) throws ManagerServiceException {
        // 数据校验
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new ManagerServiceException("账户密码为空");
        }

        ManagerUserExample example = new ManagerUserExample();
        example.or()
                .andUsernameEqualTo(username)
                .andPasswordEqualTo(password);


        List<ManagerUser> managerUsers = mapper.selectByExample(example);
        if (managerUsers.isEmpty()) {
            throw new ManagerServiceException("账户或密码错误");
        }

        return managerUsers.get(0);
    }

    @Override
    public PageInfo<ManagerUser> findAll(Integer page, Integer rows, String username) {
        PageHelper.startPage(page, rows);
        List<ManagerUser> list = mapper.selectManagerUserWithRoles(username);
        PageInfo<ManagerUser> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void update(Integer id, String username, String password, Integer supplierid, Integer[] ids) throws ManagerServiceException {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new ManagerServiceException("用户名密码为空");
        }

        ManagerUser managerUser = new ManagerUser();
        managerUser.setId(id);
        managerUser.setUsername(username);
        managerUser.setSupplierid(supplierid);
        managerUser.setPassword(password);
        mapper.updateByPrimaryKey(managerUser);

        mapper.deleteRoleManagerUser(id);

        for (int i = 0; i < ids.length; i++) {
            mapper.insertRoleManagerUser(managerUser.getId(), ids[i]);
        }


    }

    @Override
    public void add(String username, String password, Integer supplierid, Integer[] ids) throws ManagerServiceException {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new ManagerServiceException("用户名密码为空?");
        }

        ManagerUser user = new ManagerUser();
        user.setUsername(username);
        user.setPassword(password);
        user.setSupplierid(supplierid);
        mapper.insertSelective(user);

        for (int i = 0; i < ids.length; i++) {
            mapper.insertRoleManagerUser(user.getId(), ids[i]);
        }

    }

    @Override
    public void modifyPassword(Integer id, String password) throws ManagerServiceException {
        if (StringUtils.isEmpty(password)) {
            throw new ManagerServiceException("密码为空");
        }

        ManagerUser managerUser = new ManagerUser();
        managerUser.setId(id);
        managerUser.setPassword(password);

        mapper.updateByPrimaryKeySelective(managerUser);

    }
}
